package com.h.extensions.listener;

import com.h.extensions.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RetryListener {
    // auto模式的消费者
    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void handMessage(Message message) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("["+Constants.RETRY_QUEUE+"]接收到消息: %s, deliveryTag: %s /n",
                new String(message.getBody()),deliveryTag);
        int sum = 3/0;
        System.out.println("业务处理完成");
    }

//    // 手动确认模式
//    @RabbitListener(queues = Constants.RETRY_QUEUE)
//    public void handMessage(Message message,Channel channel) throws Exception{
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            System.out.printf("["+Constants.RETRY_QUEUE+"]接收到消息: %s, deliveryTag: %s /n",
//                    new String(message.getBody()),deliveryTag);
//            int sum = 3/0;
//            channel.basicAck(deliveryTag, false);
//            System.out.println("业务处理完成");
//        }catch (Exception e) {
//            channel.basicNack(deliveryTag, false, true);
//        }
//    }
}
